package com.yf.course.modules.course.service.impl;

import com.yf.course.modules.course.entity.Course;
import com.yf.course.modules.course.mapper.CourseMapper;
import com.yf.course.modules.course.service.CourseSocketService;
import com.yf.course.websocket.server.SystemEndpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import java.io.IOException;

@Service
public class CourseSocketServiceImpl implements CourseSocketService {
    @Autowired
    private SystemEndpoint systemEndpoint;

    @Autowired
    private CourseMapper courseMapper;

    /**
     * 数字人生成成功或失败的处理
     * @param courseId 课程ID
     * @param flag 成功、失败的标志
     */
    public String digitalPerson(String courseId, Boolean flag) {
        Course course = courseMapper.selectById(courseId);
        Session session = systemEndpoint.getSession(course.getCreateBy());
        if (session == null) {
            return "操作失败，" + "用户不在线";
        }
        String message = flag ? "数字人生成成功" : "数字人生成失败";
        try {
            session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            return "操作失败，" + e.getMessage();
        }
        return "操作成功";
    }

    /**
     * 知识点抽取生成或失败的处理
     * @param courseId 课程ID
     * @param flag 成功、失败的标志
     */
    public String knowledgePoint(String courseId, Boolean flag) {
        Course course = courseMapper.selectById(courseId);
        Session session = systemEndpoint.getSession(course.getCreateBy());
        if (session == null) {
            return "操作失败，" + "用户不在线";
        }
        String message = flag ? "知识点抽取成功" : "知识点抽取失败";
        try {
            session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            return "操作失败，" + e.getMessage();
        }
        return "操作成功";
    }
}
